package com.future.bantang.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.future.bantang.R;
import com.future.bantang.activity.ShoppingListActivity;
import com.future.bantang.adapter.ShoppingListAdapter;
import com.future.bantang.app.BanTangAPP;
import com.future.bantang.entity.ShoppingListEntity;
import com.future.bantang.entity.ShoppingListEntity.DataEntity.ProductEntity;
import com.future.bantang.utils.L;
import com.future.bantang.view.ScrollMonitorableScrollView;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by Administrator on 16-1-16.
 */
public class ShoppingListFragment extends BaseFragment {
    @ViewInject(R.id.lv_fragment_shopping_list_selection)
    private ListView mLvShoppingList;

    private ShoppingListAdapter mShoppingListAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shopping_list_selection, null);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mLvShoppingList.setFocusable(false);
        mShoppingListAdapter=new ShoppingListAdapter(
                getActivity(), BanTangAPP.getApp().getImageLoader());
        mLvShoppingList.setAdapter(mShoppingListAdapter);

        EventBus.getDefault().register(this);

    }


    private List<ProductEntity> productEntityList;

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void getShoppingListEntity(ShoppingListActivity.ShoppingListMsg msg){
        ShoppingListEntity entity = msg.getT();
        L.d("-----> id = "+msg.getStr() );
        final ScrollMonitorableScrollView scrollView = msg.getmScrollView();
        mShoppingListAdapter.setProductPicHost(entity.getData().getProduct_pic_host());
        mShoppingListAdapter.setUserAvatrHost(entity.getData().getUser_avatr_host());
        productEntityList = entity.getData().getProduct();
        mShoppingListAdapter.setDatas(productEntityList);
        mLvShoppingList.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem == 0) {
                    View topView = view.getChildAt(0);
                    L.d("view=" + topView);
                    if (topView.getTop() >= 0) {
                        if (scrollView != null) {
                            scrollView.setIsIntercapt(true);
                        }
                    } else {
                        if (scrollView != null) {
                            scrollView.setIsIntercapt(false);
                        }
                    }
                } else {
                    if (scrollView != null) {
                        scrollView.setIsIntercapt(false);
                    }
                }
            }
        });

        //EventBus.getDefault().removeStickyEvent(msg);


    }

    @Override
    protected void loadDatas() {
        super.loadDatas();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mShoppingListAdapter.clearDatas();
        mShoppingListAdapter=null;
        mLvShoppingList=null;
        EventBus.getDefault().unregister(this);
    }
}
